﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using X.Web;

namespace X.WebSite.Admin.News
{
    public partial class Detail : AdminPage
    {
        int _NewsId
        {
            get
            {
                int newsId = 0;

                if (!string.IsNullOrEmpty(Request.QueryString["Id"]))
                    newsId = int.Parse(Request.QueryString["Id"]);

                return newsId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                X.Entity.NewsInfo newsInfo = Bll.News.GetNewsInfo(_NewsId);

                txtTitle.Text = X.Common.StringUtils.HtmlDecode(newsInfo.Title);
                ckeContent.Text = newsInfo.Content;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            X.Entity.NewsInfo newsInfo;

            if (0 != _NewsId)
            { 
                newsInfo = Bll.News.GetNewsInfo(_NewsId);

                newsInfo.Title = X.Common.StringUtils.HtmlEncode(txtTitle.Text);
                newsInfo.Content = ckeContent.Text;
                newsInfo.PostTime = DateTime.Now;

                Bll.News.Update(newsInfo);
            }
            else
            { 
                newsInfo = new X.Entity.NewsInfo();

                newsInfo.Title = X.Common.StringUtils.HtmlEncode(txtTitle.Text);
                newsInfo.Content = ckeContent.Text;
                newsInfo.PostTime = DateTime.Now;

                Bll.News.Create(newsInfo);
            }

            Response.Redirect("List.aspx");
        }
    }
}